﻿
(function(){
	Ext.ns("Ext.fay.ui");	
	Ext.fay.ui.EditorForm=function(opt)
	{ 
	    this.addEvents("save");
	    this.initForm(opt);	   	  
	    this.loadValue(opt);
	};
    Ext.extend(Ext.fay.ui.EditorForm,Ext.util.Observable,{	
	    initForm:function(opt){
	  	 var mine=this;	  	 
	  	 var opt=Ext.apply({
		 frame: true,
		 border:false,		
		 autoWidth:true,
		 waitMsgTarget: true,  
		 layout:'form',
		 defaultType:'textfield',
		 defaults: {anchor: '90%'},
		 buttons:[{text:' 保存 ',id:'submitButton',handler:function(){
		    var v=mine.getValues();
		    v.content=mine.form.getForm().findField("content").getValue();      
		    mine.fireEvent("save",v);
		  }
		 }],
		 labelWidth:80,
		 items:[
		   {xtype:'hidden',name:'id'},
		   {xtype:'hidden',name:'addtime'},
		   {xtype:'textfield',name:'title',anchor:'97%',fieldLabel:'页面标题',allowBlank:false},
		   {xtype:'textfield',name:'keywords',anchor:'97%',fieldLabel:'页面关键字',allowBlank:false},
		   {xtype:'fieldset',layout:'fit',anchor:'100%',border:false,autoHeight:true,items:[{xtype:'fckeditor',name:'content',height:360}]}
		 ]
		 },
		 opt);
		 this.form=new Ext.form.FormPanel(opt);
		}, 
	    setSubmitButton:function(fn){
	      var bt=this.bt||Ext.getCmp("submitButton");
	      if(bt)
	      {
	         bt[fn]();
	      }
	    },   
	    loadValue:function(opt)
	    {
	      if(opt.loadParams)
	      {
	         var mine=this;
	         opt.loadParams.callback=function(v)
	         { 	                    
	             mine.form.getForm().setValues(v);
	             mine.setSubmitButton("enable");
	             return false;
	         }
	         opt.loadParams.params=opt.loadParams.params||{}
	         opt.loadParams.params["_ds"]=(new Date()).getTime();
	         this.postJson(opt.loadParams);
	      }
	    },
	    getValues:function(){
	     if(this.form) return this.form.getForm().getValues();
	     else return {};
	    },	    
	    postJson:function(opt)
	    {
               
	         //alert(opt.params._ds);
	         var mine=this;
	         this.setSubmitButton("disable");
	         Ext.Ajax.request({
		         method:'POST',
				 url:opt.url,				
				 success: function(res){	
				 	var data=Ext.util.JSON.decode(res.responseText);
				 	if(opt.callback)
				 	{
				 	  if(opt.callback.call(mine,data)===false)
				 	  {
				 	    return;
				 	  }
				 	}
				 	if(data.success)
				 	{
				 	  Ext.alert(data.info);
				 	} 
				 	else
				 	{
				 	  Ext.error(data.info);
				 	}
                    mine.setSubmitButton("enable");	
				 },
				 failure: function(){
			        Ext.error("出错了");
	                this.setSubmitButton("enable");
				 },
				 jsonData:opt.params
		     });  
	    }  
	 });

})();